# -*- coding: utf-8 -*-
"""
@Author: Logan
@Date:   2025/6/28 16:35
@Description: 
"""
import time
import requests

from base.LoginBase import LoginBase
from selenium.webdriver.common.by import By
from base.ObjectMap import ObjectMap
from common.captcha_ocr_utils import identify_verification_code
from config.yaml_config import GetConf
from logs.log import log
from common.report_add_img import add_img_path_2_report


class LoginPage(LoginBase, ObjectMap):
    def login_input_value(self, driver, input_placeholder, input_value):
        """
        登录页输入值
        :param driver:驱动对象
        :param input_placeholder:输入框提示文本(用户名or密码or验证码)
        :param input_value:输入框输入值
        :return:
        """
        log.info("输入" + input_placeholder + ":" + str(input_value))
        input_xpath = self.login_input(input_placeholder)
        return self.element_fill_value(driver, By.XPATH, input_xpath, input_value)

    def click_login(self, driver, button_name):
        """
        点击登录
        :param driver:驱动对象
        :param button_name:按钮元素
        :return:
        """
        log.info("点击" + button_name)
        button_xpath = self.login_button(button_name)
        return self.element_click(driver, By.XPATH, button_xpath)

    def login(self, driver, user, need_captcha=False, is_success=True):
        """
        封装登录方法
        :param driver: 浏览器驱动
        :param user: 登录用户
        :param need_captcha: 是否需要验证码
        :param is_success: 是否需要验证失败toast
        :return:
        """
        self.element_to_url(driver, "/login")
        username, password = GetConf().get_username_password(user)
        if need_captcha:
            time.sleep(2)
            log.info("需要验证码")
            # 点击勾选需要验证码
            self.select_need_captcha(driver)
            time.sleep(2)
            captcha_path = self.captcha()
            captcha_screenshot_path = self.element_screenshot(driver, By.XPATH, captcha_path)
            add_img_path_2_report(captcha_screenshot_path, "图像验证码")
            # 读取本地元素截图并获取验证码
            captcha_code = identify_verification_code(captcha_screenshot_path)
            # 输入验证码
            self.login_input_value(driver, "请输入验证码", captcha_code)
            time.sleep(2)
        self.login_input_value(driver, "用户名", username)
        self.login_input_value(driver, "密码", password)
        self.click_login(driver, "登录")
        self.assert_login_success(driver,is_success)

    def login_assert(self, driver, img_name):
        """
        登录后判断头像
        :param driver:
        :param img_name:
        :return:
        """
        log.info("登录后判断头像是否一致")
        return self.find_img_in_source(driver, img_name)

    def assert_login_success(self, driver, is_success=True):
        """
        验证是否登录成功
        :param driver:
        :param is_success:
        :return:
        """
        success_xpath = self.login_success()
        fail_xpath = self.login_fail()
        # 如果需要验证登录成功
        if is_success:
            self.element_appear(driver, By.XPATH, success_xpath, timeout=2)
        else:
            # 否则验证失败
            self.element_appear(driver, By.XPATH, fail_xpath, timeout=2)

    def select_need_captcha(self, driver):
        """
        点击勾选是否需要验证码
        :param driver:
        :return:
        """
        log.info("点击勾选是否需要验证码")
        select_xpath = self.need_captcha()
        return self.element_click(driver, By.XPATH, select_xpath)

    def api_login(self, driver, user):
        """
        通过api登录
        :param driver:
        :param user:
        :return:
        """
        log.info("跳转登录页")
        self.element_to_url(driver, "/login")
        username, password = GetConf().get_username_password(user)
        log.info("用户名:" + str(username))
        log.info("密码:" + str(password))
        url = GetConf().get_url()
        data = {
            "user": username,
            "password": password
        }
        log.info("通过api登录")
        res = requests.post(url + "/api/user/login", json=data)
        token = res.json()["data"]["token"]
        js_script = "window.sessionStorage.setItem('token','%s');" % token
        log.info("将token写入sessionstrage")
        driver.execute_script(js_script)
        time.sleep(2)
        log.info("跳转主页")
        self.element_to_url(driver, "/")
